<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="波形名称" prop="fWaveName">
        <el-input v-model="queryParams.fWaveName" placeholder="请输入波形程序名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="添加日期">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:post:add']">新增波形</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="waveformList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="fId" width="80" />
      <el-table-column label="波形编号" align="center" prop="fWaveCode" width="120" />
      <el-table-column label="波形名称" align="center" prop="fWaveName" width="150" />
      <el-table-column label="支持尺寸" align="center" prop="fDimensions" width="120" />
      <el-table-column label="IC 型号" align="center" prop="fIcChip" width="120" />
      <el-table-column label="温度段（℃）" align="center" prop="fMaxNum" width="150" />
      <el-table-column label="波形效果" align="center" prop="fWaveOutcome" width="150">
        <template slot-scope="scope">
          <span v-for="item in JSON.parse(scope.row.fWaveOutcome)" style="margin-right: 2px;">{{ item }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作状态" align="center" prop="fOperationStatus" width="120" /> <!-- 新增的列 -->      
      <el-table-column label="创建人" align="center" prop="fCreaterName" width="120" />
      <el-table-column label="创建日期" align="center" prop="fCreateDate" width="180">
      </el-table-column>
      <el-table-column label="最后修改人" align="center" prop="fModifierName" width="120" />
      <el-table-column label="最后修改日期" align="center" prop="fModifyDate" width="180" />
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" min-width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 1)"
            v-hasPermi="['system:post:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 0)"
            v-hasPermi="['system:post:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="65%">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="波形名称" prop="fWaveName">
          <el-input v-model="form.fWaveName" placeholder="请输入波形名称" :disabled="isEdit == 1" />
        </el-form-item>
        <el-form-item label="支持尺寸" prop="fDimensions">
          <el-input v-model="form.fDimensions" placeholder="请输入支持尺寸" :disabled="isEdit == 1" />
        </el-form-item>
        <el-form-item label="IC型号" prop="fIcChip">
          <el-input v-model="form.fIcChip" placeholder="请输入IC型号" :disabled="isEdit == 1" />
        </el-form-item>
        <el-form-item label="最高温度（℃）" prop="fMaxNum">
          <el-input v-model="form.fMaxNum" placeholder="请输入最高温度" :disabled="isEdit == 1" />
        </el-form-item>
        <el-form-item label="最低温度（℃）" prop="fMinNum">
          <el-input v-model="form.fMinNum" placeholder="请输入最低温度" :disabled="isEdit == 1" />
        </el-form-item>
        <el-form-item label="波形效果" prop="fWaveOutcome">
          <el-checkbox-group v-model="fWaveOutcomeList" @change="changeWave(fWaveOutcomeList)" :disabled="isEdit == 1">
            <el-checkbox v-for="item in fWaveOutcome" :label="item" :key="item">{{ item }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="效果描述" prop="fWaveDescribe">
          <el-input v-model="form.fWaveDescribe" type="textarea" placeholder="请输入效果描述" :disabled="isEdit == 1" />
        </el-form-item>
        <el-form-item label="波形程序">
          <template slot-scope="scope">
            <FileUpload :value="form.fWaveUrl" :limit="1" :isShowTip="false"
              @input="uploadFile($event, scope.$index)" :fileType="['bin']" :disabled="isEdit == 1" ></FileUpload>
          </template>
        </el-form-item>
        <el-form-item label="是否启用" prop="fForbidStatus">
          <el-radio-group v-model="form.fForbidStatus" :disabled="isEdit == 1">
            <el-radio v-for="dict in fForbidStatus" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="fRemark">
          <el-input v-model="form.fRemark" type="textarea" :disabled="isEdit == 1" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isEdit!=1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWave, getWave, delWave, addWave, updateWave } from "@/api/basicData/waveform";

export default {
  name: "Wave",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      dateRange: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      waveformList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // postCode: undefined,
        fWaveName: undefined,
        status: undefined
      },
      fWaveOutcome: ["R值提升", "残影改善", "角白改善"],
      fWaveOutcomeList: [],
      // 表单参数
      form: {
        fForbidStatus: 0
      },
      // 表单校验
      rules: {
        fWaveName: [
          { required: true, message: "波形名称不能为空", trigger: "blur" }
        ],
        fForbidStatus:[
          { required: true, message: "请选择是否启用不能选空", trigger: "change" }
        ],
        // postCode: [
        //   { required: true, message: "岗位编码不能为空", trigger: "blur" }
        // ],
        // postSort: [
        //   { required: true, message: "岗位顺序不能为空", trigger: "blur" }
        // ]
      },
      fForbidStatus: [
        { value: 0, label: '启用' },
        { value: 1, label: '停用' }
      ],
      isEdit: 0,
      fileList: [],

    };
  },
  created() {
    this.getList();
  },
  methods: {

    changeWave(e) {
      console.log(e)
      this.form.fWaveOutcome = e
    },
    //导入文件
    uploadFile(e,index) {
      console.log(e,index)
      // debugger
      this.$set(this.form, 'fWaveUrl', e);
            // this.form.fWaveUrl = e
            // this.waveformList[index].fPictureUrl = e
            // this.waveformList[index].fUrlState = this.waveformList[index].fPictureUrl?1:0
        },
    /** 查询岗位列表 */
    addDateRange(params, dateRange) {
      params.fCreateDateStart = dateRange[0];
      params.fCreateDateEnd = dateRange[1];
      return params;
    },
    getList() {
      this.loading = true;
      this.waveformList = []
      listWave(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.waveformList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        // postCode: undefined,
        fWaveName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined,
        fWaveOutcome: "",
        fileList: [{

        }],
        fForbidStatus:0

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isEdit = 0
      this.reset();
      this.open = true;
      this.title = "新增波形";
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.isEdit = type
      this.reset();
      const postId = row.fId || this.ids
      getWave(postId).then(response => {
        this.form = response.data;
        this.fWaveOutcomeList = JSON.parse(response.data.fWaveOutcome);
        this.open = true;
        if (type === 1) {
          this.open = true;
          this.title = "查看波形";
        } else {
          this.open = true;
          this.title = "修改波形";
        }      
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fId != undefined) {
            updateWave(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWave(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.fWaveOutcomeList = []
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.fId || this.ids;
      this.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function () {
        return delWave(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/wave/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
